#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>

using namespace std;
using LL = long long;
vector<vector<string>> num;

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, q, l;
    string s;
    cin >> n >> q;
    for(int i = 0; i < n; i ++){
        vector<string> temp;
        cin >> l;
        for(int j = 0; j < l; j ++){
            cin >> s;
            temp.push_back(s);
        }
        num.push_back(temp);
    }
    
    int s1, t1;
    while(q --){
        cin >> s1 >> t1;
        cout << num[s1 - 1][t1 - 1] << '\n';
    }

    return 0;
}